package com.indexingsystem.db.mongoDB.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;

import org.bson.Document;
import org.bson.conversions.Bson;

import com.indexingsystem.boss.entity.ResCategoryRelations;
import com.indexingsystem.boss.entity.ResourceCategors;
import com.indexingsystem.boss.logback.LogCvt;
import com.indexingsystem.db.mongoDB.MongoManager;
import com.indexingsystem.db.mongoDB.comon.MongoTableName;
import com.indexingsystem.db.mongoDB.po.ResourceCategorsResPo;
import com.mongodb.BasicDBObject;
import com.mongodb.client.model.Filters;

/**
 * 
 * @author Administrator 资源分类id获取资源列表 集合名称：dh_resource_categorys_res_list
 *         集合说明：对产品分类中产品列表信息进行冗余，便于在产品分类中获取产品信息避免多表连接。
 * 
 * 
 */
public class ResourceCategorsResListMongo {
	/**
	 * 
	 * @Title: addInstitutions @see: TODO @param Institutions @return @return
	 *         boolean 返回类型 @throws
	 */
	public static void addResourceCategorsResList(List<ResourceCategorsResPo> resourceCategorsResPoList, String institutionId) {
		MongoManager manager = new MongoManager();
		LogCvt.debug( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "添加 ------->"
				+ com.alibaba.fastjson.JSON.toJSONString( resourceCategorsResPoList, true ) );
		// List<Document> addObjList = new ArrayList<Document>();
		if (null != resourceCategorsResPoList && resourceCategorsResPoList.size() > 0) {
			for (ResourceCategorsResPo resCateResPo : resourceCategorsResPoList) {
				Document addObj = new Document();
				// 先查询如果不存在就新增，否则就不新增
				Bson bson = Filters.and( Filters.eq( "res_category_id", resCateResPo.getResCategoryId() ),
						Filters.eq( "res_id", resCateResPo.getResId() ), Filters.eq( "institutionId", institutionId ) );
				LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "获取资源分类资源列表查询条件:" + Arrays.asList( bson ).toString() );
				List<Document> documentsList = manager.queryAggregateList( bson, ResCategoryRelations.class,
						MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );

				if (null != documentsList && documentsList.size() > 0) {
					continue;
				}
				addObj.put( "institutionId", institutionId );
				addObj.put( "date_px", resCateResPo.getDatePx() );
				addObj.put( "res_id", resCateResPo.getResId() );
				addObj.put( "res_name", resCateResPo.getResName() );
				addObj.put( "publish_date", resCateResPo.getPublishDate() );
				addObj.put( "thumbnail", resCateResPo.getThumbnail() );
				addObj.put( "res_type", resCateResPo.getResType() );
				addObj.put( "res_type_id", resCateResPo.getResTypeId() );
				addObj.put( "res_category_id", resCateResPo.getResCategoryId() );
				addObj.put( "res_category_name", resCateResPo.getResCategoryName() );
				addObj.put( "period_of_time", resCateResPo.getPeriodOfTime() );
				addObj.put( "res_table", resCateResPo.getResTable() );
				addObj.put( "status", resCateResPo.getStatus() );
				// addObjList.add( addObj );
				manager.add( addObj, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST ); // 向mongodb插入数据
			}
			// manager.addList( addObjList,
			// MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST ); // 向mongodb插入数据
		}
	}

	/**
	 * 批量审核同步修改审核状态.
	 * 
	 * @param resIdList
	 * @param status
	 * @param connName
	 */
	public static void updateResourceStatus(List<String> resIdList, int status, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		for (String resId : resIdList) {
			BasicDBObject basicDBObject = new BasicDBObject();
			basicDBObject.put( "resId", resId );
			basicDBObject.put( "institutionId", institutionId );
			LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "获取资源查询条件:" + Arrays.asList( basicDBObject ).toString() );
			List<Document> documentsList = manager.queryAggregateList( basicDBObject, null, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
			if (null != documentsList && documentsList.size() > 0) {
				for (Document document : documentsList) {
					document.put( "status", status );
					Bson resbatchAudit = new Document( "$set", document );
					manager.updateOne( basicDBObject, resbatchAudit, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
				}
			}
		}
	}

	/**
	 * 通过资源分类id获取所有产品资源列表带分页数据.
	 * 
	 * @param mongoPage
	 * @param resCategoryIdList
	 * @return
	 * @throws Exception
	 */
	public static List<ResourceCategorsResPo> findResourceCategorsResListByPage(List<String> resCategoryIdList, int pageNo, int pageSize,
			String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		List<ResourceCategorsResPo> resourceCategorsResVoList = null;
		// 查询条件
		if (null != resCategoryIdList && resCategoryIdList.size() > 0) {
			// String[] resCategoryIdArr = new String[resCategoryIdList.size()];
			// resCategoryIdList.toArray( resCategoryIdArr );
			// if (resCategoryIdArr.length > 0) {
			Bson bson = Filters.and( Filters.in( "res_category_id", resCategoryIdList ), Filters.eq( "res_table", "Magazines" ),
					Filters.eq( "status", 1 ), Filters.eq( "institutionId", institutionId ) );
			int start = pageNo;
			int end = pageSize;

			Document fetchFields = new Document();
			fetchFields.put( "_id", 0 );
			fetchFields.put( "date_px", 1 );
			fetchFields.put( "res_id", 1 );
			fetchFields.put( "res_name", 1 );
			fetchFields.put( "publish_date", 1 );
			fetchFields.put( "thumbnail", 1 );
			fetchFields.put( "res_type", 1 );
			fetchFields.put( "res_type_id", 1 );
			fetchFields.put( "res_category_id", 1 );
			fetchFields.put( "res_category_name", 1 );
			fetchFields.put( "period_of_time", 1 );
			fetchFields.put( "res_table", 1 );
			fetchFields.put( "status", 1 );

			// 排序条件
			Document sortDocument = new Document();
			sortDocument.put( "publish_date", -1 );

			LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "分页获取资源分类资源列表查询条件:" + bson.toString() );
			LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "分页获取资源分类资源列表mongodb查询[" + start + "]至[" + end + "]条数据[开始...]" );
			List<Document> documentsList = manager.queryPages( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST, bson, sortDocument, fetchFields, start,
					end );

			if (null != documentsList && documentsList.size() > 0) {
				resourceCategorsResVoList = new ArrayList<ResourceCategorsResPo>();
				for (Document document : documentsList) {
					ResourceCategorsResPo resourceCategorsResPo = new ResourceCategorsResPo();
					resourceCategorsResPo.setDatePx( document.getDate( "date_px" ) );
					resourceCategorsResPo.setPeriodOfTime( document.getInteger( "period_of_time" ) );
					resourceCategorsResPo.setPublishDate( document.getString( "publish_date" ) );
					resourceCategorsResPo.setResCategoryId( document.getString( "res_category_id" ) );
					resourceCategorsResPo.setResCategoryName( document.getString( "res_category_name" ) );
					resourceCategorsResPo.setResId( document.getString( "res_id" ) );
					resourceCategorsResPo.setResName( document.getString( "res_name" ) );
					resourceCategorsResPo.setResTable( document.getString( "res_table" ) );
					resourceCategorsResPo.setResType( document.getInteger( "res_type" ) );
					resourceCategorsResPo.setResTypeId( document.getString( "res_type_id" ) );
					resourceCategorsResPo.setThumbnail( document.getString( "thumbnail" ) );
					resourceCategorsResPo.setStatus( document.getInteger( "status" ) );
					if (!resourceCategorsResVoList.contains( resourceCategorsResPo )) {
						resourceCategorsResVoList.add( resourceCategorsResPo );
					}
				}
				// }
			}
		}
		return resourceCategorsResVoList;
	}

	/**
	 * 通过资源分类id获取所有产品资源列表带分页数据.
	 * 
	 * @param mongoPage
	 * @param resCategoryIdList
	 * @return
	 * @throws Exception
	 */
	public static Integer findResourceCategorsResListCount(List<String> resCategoryIdList, String institutionId) throws Exception {
		MongoManager manager = new MongoManager();
		// 查询条件
		Integer maxCount = 0;
		if (null != resCategoryIdList && resCategoryIdList.size() > 0) {
			// String[] resCategoryIdArr = new String[resCategoryIdList.size()];
			// resCategoryIdList.toArray( resCategoryIdArr );

			// if (resCategoryIdArr.length > 0) {
			Bson bson = Filters.and( Filters.in( "res_category_id", resCategoryIdList ), Filters.eq( "res_table", "Magazines" ),
					Filters.eq( "status", 1 ), Filters.eq( "institutionId", institutionId ) );

			LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "分页获取资源分类资源列表查询条件:" + Arrays.asList( bson ).toString() );
			List<Document> documentsList = manager.queryAggregateList( bson, ResourceCategorsResPo.class,
					MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
			if (null != documentsList) {
				if (documentsList.size() > 0) {
					maxCount = documentsList.size();
				}
				// }
			}
		}
		return maxCount;
	}

	/**
	 * 根据资源id，资源分类id查询
	 * 
	 * @param resId
	 * @param resCategoryId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public static List<ResourceCategorsResPo> findResourceCategorsListById(ResCategoryRelations resCategoryRelations, String institutionId) {
		MongoManager manager = new MongoManager();
		List<ResourceCategorsResPo> resourceCategorsResVoList = null;
		if (null != resCategoryRelations) {
			Bson bson = Filters.and( Filters.eq( "res_category_id", resCategoryRelations.getResCategoryId() ),
					Filters.eq( "res_id", resCategoryRelations.getResId() ), Filters.eq( "institutionId", institutionId ) );
			LogCvt.info( MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST + "获取资源分类资源列表查询条件:" + Arrays.asList( bson ).toString() );

			List<Document> documentsList = manager.queryAggregateList( bson, ResCategoryRelations.class,
					MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
			if (null != documentsList && documentsList.size() > 0) {
				resourceCategorsResVoList = new ArrayList<ResourceCategorsResPo>();
				for (Document document : documentsList) {
					ResourceCategorsResPo resourceCategorsResPo = new ResourceCategorsResPo();
					resourceCategorsResPo.setDatePx( document.getDate( "date_px" ) );
					resourceCategorsResPo.setPeriodOfTime( document.getInteger( "period_of_time" ) );
					resourceCategorsResPo.setPublishDate( document.getString( "publish_date" ) );
					resourceCategorsResPo.setResCategoryId( document.getString( "res_category_id" ) );
					resourceCategorsResPo.setResCategoryName( document.getString( "res_category_name" ) );
					resourceCategorsResPo.setResId( document.getString( "res_id" ) );
					resourceCategorsResPo.setResName( document.getString( "res_name" ) );
					resourceCategorsResPo.setResTable( document.getString( "res_table" ) );
					resourceCategorsResPo.setResType( document.getInteger( "res_type" ) );
					resourceCategorsResPo.setResTypeId( document.getString( "res_type_id" ) );
					resourceCategorsResPo.setThumbnail( document.getString( "thumbnail" ) );
					resourceCategorsResPo.setStatus( document.getInteger( "status" ) );
					if (!resourceCategorsResVoList.contains( resourceCategorsResPo )) {
						resourceCategorsResVoList.add( resourceCategorsResPo );
					}
				}
			}
		}
		return resourceCategorsResVoList;
	}

	/**
	 * 根据资源id，资源分类id删除
	 * 
	 * @param resCategoryRelationId
	 */
	public static void deleteResCategoryRelationsById(ResCategoryRelations resCategoryRelations, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.eq( "res_category_id", resCategoryRelations.getResCategoryId() ),
				Filters.eq( "res_id", resCategoryRelations.getResId() ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteOne( bson, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
	}

	/**
	 * 根据资源id，资源分类id删除
	 * 
	 * @param resCategoryRelationId
	 */
	public static void deleteResCategoryRelationsByResId(List<String> resIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "res_id", resIdList ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteOne( bson, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
	}

	/**
	 * 根据分类id修改
	 * 
	 * @param record
	 */
	public static void updateResCategorRelationsName(ResourceCategors record, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.eq( "res_category_id", record.getResCategoryId() ), Filters.eq( "institutionId", institutionId ) );
		Document doc = new Document( "$set", new Document( "res_category_name", record.getResName() ) );
		manager.updateAll( bson, doc, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
	}

	/**
	 * 根据分类id删除资源关系表
	 * 
	 * @param resCategoryId
	 */
	public static void deleteResCategoryRelationsByResCategoryId(List<String> resCategoryIdList, String institutionId) {
		MongoManager manager = new MongoManager();
		Bson bson = Filters.and( Filters.in( "res_category_id", resCategoryIdList ), Filters.eq( "institutionId", institutionId ) );
		manager.deleteAll( bson, MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );
	}

	/**
	 * 根据资源id，资源分类id查询
	 * 
	 * @param resId
	 * @param resCategoryId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public static List<String> selectResourceCategorysForMongo(String resCategoryId, Integer year, String institutionId) {
		List<String> years = new ArrayList<String>();
		MongoManager manager = new MongoManager();
		List<ResourceCategorsResPo> resourceCategorsResVoList = null;
		if (null != resCategoryId) {

			BasicDBObject basicDBObject = new BasicDBObject();
			basicDBObject.put( "res_category_id", resCategoryId );
			basicDBObject.put( "res_table", "Magazines" );
			basicDBObject.put( "institutionId", institutionId );
			// 模糊查詢
			if (null != year && year > 0) {
				Pattern pattern = Pattern.compile( ".*" + year + ".*", Pattern.CASE_INSENSITIVE );
				basicDBObject.put( "publish_date", pattern );
			}

			List<Document> documentsList = manager.queryAggregateList( basicDBObject, ResCategoryRelations.class,
					MongoTableName.DH_RESOURCE_CATEGORYS_RES_LIST );

			if (null != documentsList && documentsList.size() > 0) {
				resourceCategorsResVoList = new ArrayList<ResourceCategorsResPo>();
				for (Document document : documentsList) {
					ResourceCategorsResPo resourceCategorsResPo = new ResourceCategorsResPo();
					resourceCategorsResPo.setDatePx( document.getDate( "date_px" ) );
					resourceCategorsResPo.setPeriodOfTime( document.getInteger( "period_of_time" ) );
					resourceCategorsResPo.setPublishDate( document.getString( "publish_date" ) );
					resourceCategorsResPo.setResCategoryId( document.getString( "res_category_id" ) );
					resourceCategorsResPo.setResCategoryName( document.getString( "res_category_name" ) );
					resourceCategorsResPo.setResId( document.getString( "res_id" ) );
					resourceCategorsResPo.setResName( document.getString( "res_name" ) );
					resourceCategorsResPo.setResTable( document.getString( "res_table" ) );
					resourceCategorsResPo.setResType( document.getInteger( "res_type" ) );
					resourceCategorsResPo.setResTypeId( document.getString( "res_type_id" ) );
					resourceCategorsResPo.setThumbnail( document.getString( "thumbnail" ) );
					resourceCategorsResPo.setStatus( document.getInteger( "status" ) );
					/*
					 * if (!resourceCategorsResVoList.contains(
					 * resourceCategorsResPo )) { resourceCategorsResVoList.add(
					 * resourceCategorsResPo ); }
					 */
					// 年份分组
					String tempYesr = resourceCategorsResPo.getPublishDate().substring( 0, 4 );
					if (!years.contains( tempYesr )) {
						years.add( tempYesr );
					}

				}
			}
		}
		return years;
	}
}